
import pymysql

class MysqlAccess():

    def __init__(self):
        self.host = 'localhost'
        self.user = 'root'
        self.passwd = '123456'
        self.db = 'db_library'
        self.port = 3306
        self.charset = 'utf8'

    def query(self,sql_cmd,params):
        try:
            conn = pymysql.connect(
                host = self.host,
                user = self.user,
                passwd = self.passwd,
                db = self.db,
                port = self.port,
                charset = self.charset
            )
            cur = conn.cursor()
            if params:
                cur.execute(sql_cmd, params)
            else:
                cur.execute(sql_cmd)
            ret = cur.fetchall()
            conn.commit()
        except Exception as e:
            print(e)
        finally:
            if cur:
                cur.close()
            if conn:
                conn.close()
        return ret

a = MysqlAccess()
a.query("DELETE FROM test WHERE NAME=(%s) and (age=(%s) and gender=(%s))",('董','10','男'))